package cn.boodqian.d3surveyor;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import cn.boodqian.util.Log;
import android.content.Intent;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Toast;

public class ChoosePartFragment extends Fragment implements OnTouchListener {

	private long touchDownTime = 0;
	private HashMap<Integer, RectF> mPartRectMap = new HashMap<Integer, RectF>(); 

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		View image = getActivity().findViewById(R.id.image_body);
		image.setOnTouchListener(this);
		
		mPartRectMap.put(100, new RectF(7f/259, 246f/363, 61f/259, 356f/363)); //Weapon
		mPartRectMap.put(400, new RectF(195f/259, 246f/363, 250f/259, 356f/363)); //Second-hand
		mPartRectMap.put(1100, new RectF(29f/259, 23f/363, 84f/259, 97f/363)); //Shoulder
		mPartRectMap.put(1000, new RectF(100f/259, 4f/363, 155f/259, 58f/363)); //Head
		mPartRectMap.put(1200, new RectF(94f/259, 64f/363, 163f/259, 160f/363)); //Torso
		mPartRectMap.put(1300, new RectF(195f/259, 109f/363, 250f/259, 184f/363)); //Bracer
		mPartRectMap.put(1400, new RectF(7f/259, 109f/363, 61f/259, 184f/363)); //Gloves
		mPartRectMap.put(1500, new RectF(94f/259, 167f/363, 163f/259, 194f/363)); //Waist
		mPartRectMap.put(1600, new RectF(100f/259, 200f/363, 155f/259, 275f/363)); //Legs
		mPartRectMap.put(1700, new RectF(100f/259, 281f/363, 155f/259, 355f/363)); //Boot
		mPartRectMap.put(1800, new RectF(175f/259, 41f/363, 219f/259, 86f/363)); //Amulet
		mPartRectMap.put(1900, new RectF(18f/259, 197f/363, 51f/259, 231f/363)); //Ring1
		mPartRectMap.put(1900, new RectF(206f/259, 197f/363, 240f/259, 231f/363)); //Ring2
		
		D3DB mDB = D3DB.getInstance(getActivity());
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.choosepart_fragment, container, false);
	}

	public boolean onTouch(View v, MotionEvent event) {
		boolean handled = false;

		int action = event.getAction();
		Log.i("onTouch:"+action);
		int eType = -1;
		switch (action){
		case MotionEvent.ACTION_DOWN:
			touchDownTime  = SystemClock.elapsedRealtime();
			break;

		case MotionEvent.ACTION_UP:
			//if (SystemClock.elapsedRealtime() - touchDownTime <= 100)
			{
				float x = event.getX();
				float y = event.getY();
				eType = pointToEquipType(x, y, v.getWidth(), v.getHeight());
				handled = true;
			}
			break;
		}
		
		Log.i("EType="+eType);
		if(eType==100) {
			Toast toast = Toast.makeText(getActivity().getApplicationContext(),
					"Weapon will be supported in next version", 
					Toast.LENGTH_SHORT);
			// toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
		} else if(eType>0) {
			Intent intent = new Intent(getActivity().getApplicationContext(),
					MagicPropertyActivity.class);
			intent.putExtra("e_type", eType);
			D3DB mDB = D3DB.getInstance(getActivity());
			// set part
			mDB.setPartType(eType);
			startActivity(intent);
		}

		return true;

	}
	
	private int pointToEquipType(float x, float y, float w_view, float h_view) {
		BitmapDrawable d = (BitmapDrawable)getActivity().getResources().getDrawable(R.drawable.body_witch);
		int h_draw = d.getBitmap().getHeight(), w_draw = d.getBitmap().getWidth();
		Log.i("Drawable size: "+w_draw+"x"+h_draw);
		Log.i("Click pos: "+x+", "+y);
		Log.i("View size: "+w_view+"x"+h_view);
		Iterator<Entry<Integer, RectF>> it = mPartRectMap.entrySet().iterator();
		while(it.hasNext()) {
			Entry<Integer, RectF> e = it.next();
			if(e.getValue().contains(x/w_view, y/h_view))
				return e.getKey()/100*100;
		}
		return -1;
	}

}
